using System;
using System.Collections;
using Microsoft.Win32;

namespace OpenEdge.Utility
{

	public class ProjectRegistry: IDisposable
	{
		#region IDisposable Members

		public void Dispose()
		{
			RegistryKey registryKey = Registry.CurrentUser;
			registryKey = registryKey.OpenSubKey("Software").OpenSubKey("Sparx Systems").OpenSubKey("EAAddins").OpenSubKey("OpenEdge", true);

			foreach (DictionaryEntry entry in registry) 
			{
				registryKey.SetValue((string) entry.Key, entry.Value);				
			}

		}

		#endregion


		private static ProjectRegistry instance;
		private static int numOfReference;

		private DateTime instanceDateTime;  
		private EA.Repository repository;
		private Hashtable registry;

		public ProjectRegistry(EA.Repository repository)
		{
			numOfReference = 0;
			instanceDateTime = DateTime.Now;
			this.repository = repository;
			
			registry = new Hashtable();

			RegistryKey registryKey = Registry.CurrentUser;
			registryKey = registryKey.OpenSubKey("Software").OpenSubKey("Sparx Systems").OpenSubKey("EAAddins").OpenSubKey("OpenEdge");

			string[] valueNames = registryKey.GetValueNames();
			for (short idx = 0; idx < registryKey.ValueCount; idx++)
			{
				registry.Add(valueNames[idx], registryKey.GetValue(valueNames[idx]));
			}

		}

		public static ProjectRegistry GetInstance(EA.Repository repository)
		{
			if (instance == null)
			{
				instance = new ProjectRegistry(repository);
			}
			numOfReference++;
			return instance;
		}            

		public static void DeleteInstance()
		{
			if (instance != null)
			{
				instance.Dispose();
				instance = null;
			}
		}            

		public static int Reference
		{
			get { return numOfReference; }
		}  

		public DateTime InstanceDateTime
		{
			get { return instanceDateTime; }
		}

		public Object GetValue(String key)
		{
			return registry[key];
		}

		public void SetValue(String key, Object thisValue)
		{
			registry[key] = thisValue;
		}

	}
}
